import Mock from 'mockjs';
import setupMock, {
  successResponseWrap,
  failResponseWrap,
} from '@/utils/setup-mock';

import { MockParams } from '@/types/mock';
import { isLogin } from '@/utils/auth';

setupMock({
  setup() {
    // Mock.XHR.prototype.withCredentials = true;

    // 用户信息
    Mock.mock(new RegExp('/api/user/info'), () => {
      if (isLogin()) {
        const role = window.localStorage.getItem('userRole') || 'admin';
        return successResponseWrap({
          name: 'admin',
          avatar:
            '//cdn.dancf.com/fe-assets/20250424/3277dc6bd2124e104a49b0a0b6a95997.svg',
          email: 'wangliqun@email.com',
          job: 'frontend',
          jobName: '前端艺术家',
          organization: 'Frontend',
          organizationName: '前端',
          location: 'beijing',
          locationName: '北京',
          introduction: '人潇洒，性温存',
          personalWebsite: 'https://www.arco.design',
          phone: '150****0000',
          registrationDate: '2013-05-10 12:10:00',
          accountId: '15012312300',
          certification: 1,
          role,
        });
      }
      return failResponseWrap(null, '未登录', 50008);
    });

    // 登录
    Mock.mock(new RegExp('/api/user/login'), (params: MockParams) => {
      const { username, password } = JSON.parse(params.body);
      if (!username) {
        return failResponseWrap(null, '用户名不能为空', 50000);
      }
      if (!password) {
        return failResponseWrap(null, '密码不能为空', 50000);
      }
      if (username === 'admin' && password === 'admin') {
        window.localStorage.setItem('userRole', 'admin');
        return successResponseWrap({
          token: '12345',
        });
      }
      if (username === 'user' && password === 'user') {
        window.localStorage.setItem('userRole', 'user');
        return successResponseWrap({
          token: '54321',
        });
      }
      return failResponseWrap(null, '账号或者密码错误', 50000);
    });

    // 登出
    Mock.mock(new RegExp('/api/user/logout'), () => {
      return successResponseWrap(null);
    });

    // 用户的服务端菜单
    Mock.mock(new RegExp('/api/user/menu'), () => {
      const menuList = [
        {
          path: '/dashboard',
          name: 'dashboard',
          meta: {
            locale: 'menu.dashboard',
            requiresAuth: true,
            icon: 'icon-dashboard',
            order: 0,
          },
          children: [
            {
              path: 'workplace',
              name: 'Workplace',
              meta: {
                locale: 'menu.dashboard.workplace',
                requiresAuth: true,
              },
            },
            {
              path: 'monitor',
              name: 'Monitor',
              meta: {
                locale: 'menu.dashboard.monitor',
                requiresAuth: true,
              },
            },
          ],
        },
        {
          path: '/visualization',
          name: 'visualization',
          meta: {
            locale: 'menu.visualization',
            requiresAuth: true,
            icon: 'icon-apps',
            order: 1,
          },
          children: [
            {
              path: 'data-analysis',
              name: 'DataAnalysis',
              meta: {
                locale: 'menu.visualization.dataAnalysis',
                requiresAuth: true,
              },
            },
            {
              path: 'multi-dimension-data-analysis',
              name: 'MultiDimensionDataAnalysis',
              meta: {
                locale: 'menu.visualization.multiDimensionDataAnalysis',
                requiresAuth: true,
              },
            },
          ],
        },
        {
          path: '/list',
          name: 'list',
          meta: {
            locale: 'menu.list',
            requiresAuth: true,
            icon: 'icon-list',
            order: 2,
          },
          children: [
            {
              path: 'search-table',
              name: 'SearchTable',
              meta: {
                locale: 'menu.list.searchTable',
                requiresAuth: true,
              },
            },
            {
              path: 'card',
              name: 'Card',
              meta: {
                locale: 'menu.list.cardList',
                requiresAuth: true,
              },
            },
          ],
        },
        {
          path: '/form',
          name: 'form',
          meta: {
            locale: 'menu.form',
            requiresAuth: true,
            icon: 'icon-settings',
            order: 3,
          },
          children: [
            {
              path: 'step',
              name: 'Step',
              meta: {
                locale: 'menu.form.step',
                requiresAuth: true,
              },
            },
            {
              path: 'group',
              name: 'Group',
              meta: {
                locale: 'menu.form.group',
                requiresAuth: true,
              },
            },
          ],
        },
        {
          path: '/profile',
          name: 'profile',
          meta: {
            locale: 'menu.profile',
            requiresAuth: true,
            icon: 'icon-file',
            order: 4,
          },
          children: [
            {
              path: 'basic',
              name: 'Basic',
              meta: {
                locale: 'menu.profile.basic',
                requiresAuth: true,
              },
            },
          ],
        },
        {
          path: '/result',
          name: 'result',
          meta: {
            locale: 'menu.result',
            requiresAuth: true,
            icon: 'icon-check-circle',
            order: 5,
          },
          children: [
            {
              path: 'success',
              name: 'Success',
              meta: {
                locale: 'menu.result.success',
                requiresAuth: true,
              },
            },
            {
              path: 'error',
              name: 'Error',
              meta: {
                locale: 'menu.result.error',
                requiresAuth: true,
              },
            },
          ],
        },
        {
          path: '/exception',
          name: 'exception',
          meta: {
            locale: 'menu.exception',
            requiresAuth: true,
            icon: 'icon-exclamation-circle',
            order: 6,
          },
          children: [
            {
              path: '403',
              name: '403',
              meta: {
                locale: 'menu.exception.403',
                requiresAuth: true,
              },
            },
            {
              path: '404',
              name: '404',
              meta: {
                locale: 'menu.exception.404',
                requiresAuth: true,
              },
            },
            {
              path: '500',
              name: '500',
              meta: {
                locale: 'menu.exception.500',
                requiresAuth: true,
              },
            },
          ],
        },
        {
          path: '/user',
          name: 'user',
          meta: {
            locale: 'menu.user',
            icon: 'icon-user',
            requiresAuth: true,
            order: 7,
          },
          children: [
            {
              path: 'info',
              name: 'Info',
              meta: {
                locale: 'menu.user.info',
                requiresAuth: true,
              },
            },
            {
              path: 'setting',
              name: 'Setting',
              meta: {
                locale: 'menu.user.setting',
                requiresAuth: true,
              },
            },
          ],
        },
      ];
      return successResponseWrap(menuList);
    });
  },
});
